﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRSystem.Entity;
using MySql.Data.MySqlClient;
using HRSystem.Utility;

namespace HRSystem.DBAccess
{
    public class OperateProcessRecodeAccess : Access
    {
        public List<OperateProcessRecode> GetOperateProcessRecodes(int baseInfoID,int type)
        {
            string sql = "select * from operation_record where person_id=" + baseInfoID + " and " + "type=" + type;

            List<OperateProcessRecode> list = new List<OperateProcessRecode>();
            using (MySqlDataReader reader = MySqlUtil.GetDataReader(sql,null))
            {
                while (reader.Read())
                {
                    OperateProcessRecode operateProcessRecode = new OperateProcessRecode();
                    operateProcessRecode.ID = CheckIntNull(reader["id"]);
                    operateProcessRecode.PhoneTime = CheckDatetimeNull(reader["phone_time"]);
                    operateProcessRecode.PhoneOperatePerson = CheckNull(reader["phone_operate_person"]);
                    operateProcessRecode.PhoneContent = CheckNull(reader["phone_content"]);
                    operateProcessRecode.PhoneResult = CheckNull(reader["phone_result"]);
                    operateProcessRecode.IsRecommen = Convert.ToBoolean(CheckIntNull(reader["is_recommen"]));
                    operateProcessRecode.RecommenTime = CheckDatetimeNull(reader["recommen_time"]);
                    operateProcessRecode.RecommenOperatePerson = CheckNull(reader["recommen_operate_person"]);
                    operateProcessRecode.RecommenCompany = CheckNull(reader["recommen_company"]);
                    operateProcessRecode.RecommenPosition = CheckNull(reader["recommen_position"]);
                    operateProcessRecode.RecommenResult = CheckNull(reader["recommen_result"]);
                    operateProcessRecode.IsWritenTest = Convert.ToBoolean(CheckIntNull(reader["is_writen_test"]));
                    operateProcessRecode.WritenTestTime = CheckDatetimeNull(reader["writen_test_time"]);
                    operateProcessRecode.WritenTestOperatePerson = CheckNull(reader["writen_test_operate_person"]);
                    operateProcessRecode.WritenTestSubject = CheckNull(reader["writen_test_subject"]);
                    operateProcessRecode.WritenTestResult = CheckNull(reader["writen_test_result"]);
                    operateProcessRecode.IsInterview = Convert.ToBoolean(CheckIntNull(reader["is_interview"]));
                    operateProcessRecode.InterviewTime = CheckDatetimeNull(reader["interview_time"]);
                    operateProcessRecode.InterviewOperatePerson = CheckNull(reader["interview_operate_person"]);
                    operateProcessRecode.InterviewPerson = CheckNull(reader["interview_person"]);
                    operateProcessRecode.InterviewCompany = CheckNull(reader["interview_company"]);
                    operateProcessRecode.InterviewPosition = CheckNull(reader["interview_position"]);
                    operateProcessRecode.InterviewCharacter = CheckNull(reader["interview_character"]);
                    operateProcessRecode.InterviewResult = CheckNull(reader["interview_result"]);
                    operateProcessRecode.IsSalaryCommunications = Convert.ToBoolean(CheckIntNull(reader["is_salary_communications"]));
                    operateProcessRecode.SalaryCommunicationsTime = CheckDatetimeNull(reader["salary_communications_time"]);
                    operateProcessRecode.SalaryCommunicationsOperatePerson = CheckNull(reader["salary_communications_operate_person"]);
                    operateProcessRecode.SalaryCommunications = CheckNull(reader["salary_communications"]);
                    operateProcessRecode.SalaryCommunicationsResult = CheckNull(reader["salary_communications_result"]);
                    operateProcessRecode.BaseInfoID = CheckIntNull(reader["person_id"]);
                    list.Add(operateProcessRecode);
                }
            }
            return list;
        }

        public bool AddOrUpdateOperateProcessRecode(OperateProcessRecode operateProcessRecode)
        {
            bool bNew = false;
            if (operateProcessRecode.ID == 0)
            {
                bNew = true;
            }

            string sql = string.Empty;
            MySqlParameter[] myParams;

            if (bNew)
            {
                sql = @"insert operation_record(phone_time,phone_operate_person,phone_content,phone_result,is_recommen,recommen_time,recommen_operate_person,recommen_company,
                            recommen_position,recommen_result,is_writen_test,writen_test_time,
                            writen_test_operate_person,writen_test_subject,writen_test_result,is_interview,interview_time,
                            interview_operate_person,interview_person,interview_company,interview_position,
                            interview_character,interview_result,is_salary_communications,
                            salary_communications_time,salary_communications_operate_person,salary_communications,
                            salary_communications_result,person_id,type) values(
                            ?phone_time,?phone_operate_person,?phone_content,?phone_result,?is_recommen,?recommen_time,?recommen_operate_person,?recommen_company,
                            ?recommen_position,?recommen_result,?is_writen_test,?writen_test_time,
                            ?writen_test_operate_person,?writen_test_subject,?writen_test_result,?is_interview,?interview_time,
                            ?interview_operate_person,?interview_person,?interview_company,?interview_position,
                            ?interview_character,?interview_result,?is_salary_communications,
                            ?salary_communications_time,?salary_communications_operate_person,?salary_communications,
                            ?salary_communications_result,?person_id,?type)";
                myParams = new MySqlParameter[]{
                    new MySqlParameter("?phone_time",operateProcessRecode.PhoneTime),
                    new MySqlParameter("?phone_operate_person",operateProcessRecode.PhoneOperatePerson),
                    new MySqlParameter("?phone_content",operateProcessRecode.PhoneContent),
                    new MySqlParameter("?phone_result",operateProcessRecode.PhoneResult),
                    new MySqlParameter("?is_recommen",operateProcessRecode.IsRecommen),
                    new MySqlParameter("?recommen_time",operateProcessRecode.RecommenTime),
                    new MySqlParameter("?recommen_operate_person",operateProcessRecode.RecommenOperatePerson),
                    new MySqlParameter("?recommen_company",operateProcessRecode.RecommenCompany),
                    new MySqlParameter("?recommen_position",operateProcessRecode.RecommenPosition),
                    new MySqlParameter("?recommen_result",operateProcessRecode.RecommenResult),
                    new MySqlParameter("?is_writen_test",operateProcessRecode.IsWritenTest),
                    new MySqlParameter("?writen_test_time",operateProcessRecode.WritenTestTime),
                    new MySqlParameter("?writen_test_operate_person",operateProcessRecode.WritenTestOperatePerson),
                    new MySqlParameter("?writen_test_subject",operateProcessRecode.WritenTestSubject),
                    new MySqlParameter("?writen_test_result",operateProcessRecode.WritenTestResult),
                    new MySqlParameter("?is_interview",operateProcessRecode.IsInterview),
                    new MySqlParameter("?interview_time",operateProcessRecode.InterviewTime),
                    new MySqlParameter("?interview_operate_person",operateProcessRecode.InterviewOperatePerson),
                    new MySqlParameter("?interview_person",operateProcessRecode.InterviewPerson),
                    new MySqlParameter("?interview_company",operateProcessRecode.InterviewCompany),
                    new MySqlParameter("?interview_position",operateProcessRecode.InterviewPosition),
                    new MySqlParameter("?interview_character",operateProcessRecode.InterviewCharacter),
                    new MySqlParameter("?interview_result",operateProcessRecode.InterviewResult),
                    new MySqlParameter("?is_salary_communications",operateProcessRecode.IsSalaryCommunications),
                    new MySqlParameter("?salary_communications_time",operateProcessRecode.SalaryCommunicationsTime),
                    new MySqlParameter("?salary_communications_operate_person",operateProcessRecode.SalaryCommunicationsOperatePerson),
                    new MySqlParameter("?salary_communications",operateProcessRecode.SalaryCommunications),
                    new MySqlParameter("?salary_communications_result",operateProcessRecode.SalaryCommunicationsResult),
                    new MySqlParameter("person_id",operateProcessRecode.BaseInfoID),                                                    
                    new MySqlParameter("type",operateProcessRecode.type)   
                };
            }
            else
            {
                sql = @"update operation_record set   phone_time=?phone_time,phone_operate_person=?phone_operate_person,
                        phone_content=?phone_content,phone_result=?phone_result,is_recommen=?is_recommen,recommen_time=?recommen_time,
                        recommen_operate_person=?recommen_operate_person,recommen_company=?recommen_company,
                        recommen_position=?recommen_position,recommen_result=?recommen_result,
                        is_writen_test=?is_writen_test,writen_test_time=?writen_test_time,
                        writen_test_operate_person=?writen_test_operate_person,writen_test_subject=?writen_test_subject,
                        writen_test_result=?writen_test_result,is_interview=?is_interview,interview_time=?interview_time,
                        interview_operate_person=?interview_operate_person,interview_person=?interview_person,
                        interview_company=?interview_company,interview_position=?interview_position,interview_character=?interview_character,
                        interview_result=?interview_result,
                        is_salary_communications=?is_salary_communications,salary_communications_time=?salary_communications_time,
                        salary_communications_operate_person=?salary_communications_operate_person,
                        salary_communications=?salary_communications,salary_communications_result=?salary_communications_result,
                        person_id=?person_id where id=?id and type=?type";
                myParams = new MySqlParameter[]{
                    new MySqlParameter("?phone_time",operateProcessRecode.PhoneTime),
                    new MySqlParameter("?phone_operate_person",operateProcessRecode.PhoneOperatePerson),
                    new MySqlParameter("?phone_content",operateProcessRecode.PhoneContent),
                    new MySqlParameter("?phone_result",operateProcessRecode.PhoneResult),
                    new MySqlParameter("?is_recommen",operateProcessRecode.IsRecommen),
                    new MySqlParameter("?recommen_time",operateProcessRecode.RecommenTime),
                    new MySqlParameter("?recommen_operate_person",operateProcessRecode.RecommenOperatePerson),
                    new MySqlParameter("?recommen_company",operateProcessRecode.RecommenCompany),
                    new MySqlParameter("?recommen_position",operateProcessRecode.RecommenPosition),
                    new MySqlParameter("?recommen_result",operateProcessRecode.RecommenResult),
                    new MySqlParameter("?is_writen_test",operateProcessRecode.IsWritenTest),
                    new MySqlParameter("?writen_test_time",operateProcessRecode.WritenTestTime),
                    new MySqlParameter("?writen_test_operate_person",operateProcessRecode.WritenTestOperatePerson),
                    new MySqlParameter("?writen_test_subject",operateProcessRecode.WritenTestSubject),
                    new MySqlParameter("?writen_test_result",operateProcessRecode.WritenTestResult),
                    new MySqlParameter("?is_interview",operateProcessRecode.IsInterview),
                    new MySqlParameter("?interview_time",operateProcessRecode.InterviewTime),
                    new MySqlParameter("?interview_operate_person",operateProcessRecode.InterviewOperatePerson),
                    new MySqlParameter("?interview_person",operateProcessRecode.InterviewPerson),
                    new MySqlParameter("?interview_company",operateProcessRecode.InterviewCompany),
                    new MySqlParameter("?interview_position",operateProcessRecode.InterviewPosition),
                    new MySqlParameter("?interview_character",operateProcessRecode.InterviewCharacter),
                    new MySqlParameter("?interview_result",operateProcessRecode.InterviewResult),
                    new MySqlParameter("?is_salary_communications",operateProcessRecode.IsSalaryCommunications),
                    new MySqlParameter("?salary_communications_time",operateProcessRecode.SalaryCommunicationsTime),
                    new MySqlParameter("?salary_communications_operate_person",operateProcessRecode.SalaryCommunicationsOperatePerson),
                    new MySqlParameter("?salary_communications",operateProcessRecode.SalaryCommunications),
                    new MySqlParameter("?salary_communications_result",operateProcessRecode.SalaryCommunicationsResult),
                    new MySqlParameter("person_id",operateProcessRecode.BaseInfoID),                                                 
                    new MySqlParameter("type",operateProcessRecode.type),
                    new MySqlParameter("?id",operateProcessRecode.ID)
                };
            }

            if (MySqlUtil.ExecuteNonQuery(sql,myParams) == -1)
            {
                return false;
            }

            operateProcessRecode.ID = bNew ? GetLastID("operation_record") : operateProcessRecode.ID;
            return true;
        }
    }
}
